package Game;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.*;

// 主类，用于启动游戏
public class Main{
    static int BestScore;
    private static JFrame frame;
    private static GameMainGUI gameMainGUI;
    private static GameLogic gameLogic;
    private static GameMenuGUI gameMenuGUI;
    private static final String BestScorePath = "bestscore.txt";



    // 主方法，程序入口
    public static void main(String []args){
        try {
            // 检查文件是否存在
            File bestScoreFile = new File(BestScorePath);
            if (!bestScoreFile.exists()) {
                // 文件不存在，创建新文件，并设置默认最佳分数为0
                bestScoreFile.createNewFile();
                BestScore = 0;
                // 写入默认最佳分数到文件
                replaceNumber(BestScore);
            } else {
                // 文件存在，读取最佳分数
                BestScore = readNumber();
            }
        } catch (IOException e) {
            System.err.println("无法读取或创建最佳分数文件: " + e.getMessage()); // 提供有意义的错误信息
            BestScore = 0; // 默认最佳分数为0，如果需要可以设置其他默认值
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Main gameStart = new Main();
                gameStart.GameStart();
            }
        });
    }

    // 游戏启动方法，初始化游戏状态、逻辑、规则和界面
    void GameStart(){
        CardLayout cardLayout = new CardLayout();
        JPanel mainPanel = new JPanel(cardLayout);
        gameMenuGUI = new GameMenuGUI(BestScore);

        frame = new JFrame("矿工大冒险");
        frame.setSize(800, 1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        mainPanel.add(gameMenuGUI.getMenuPanel(), "main");

        gameMenuGUI.setStartButtonListener(event -> {
            gameLogic = new GameLogic(BestScore);
            gameMainGUI = new GameMainGUI(gameLogic, act -> {
                BestScore = Math.max(BestScore, gameLogic.getState().getScore());
                try {
                    replaceNumber(BestScore);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                gameMenuGUI.updateBestScore(BestScore);
                cardLayout.show(mainPanel, "main");
            });
            mainPanel.add(gameMainGUI.getGamePanel(), "game");
            gameMainGUI.updateGameState();
            gameMainGUI.setupKeyBindings();
            cardLayout.show(mainPanel, "game");
            gameMainGUI.getGamePanel().requestFocusInWindow();
        });
        cardLayout.show(mainPanel, "main");
        frame.add(mainPanel);
        frame.setVisible(true);
        frame.add(createMenuBar(), BorderLayout.NORTH);
    }

    // 读取文件中的数字
    public static int readNumber() throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(BestScorePath))) {
            String line = reader.readLine();
            return Integer.parseInt(line);
        }
    }

    // 替换文件中的数字
    public static void replaceNumber(int newNumber) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(BestScorePath))) {
            writer.write(String.valueOf(newNumber));
        }
    }

    public JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu helpMenu = new JMenu("帮助");
        JMenuItem helpMenuItem = new JMenuItem("显示帮助"); // 添加一个菜单项
        helpMenu.add(helpMenuItem); // 将菜单项添加到菜单中
        menuBar.add(helpMenu);

        helpMenuItem.addActionListener(e -> { // 将ActionListener添加到菜单项中
            showHelpDialog();
        });

        return menuBar;
    }

    private void showHelpDialog() {
        JFrame helpFrame = new JFrame("帮助文档");
        helpFrame.setSize(400, 300);
        helpFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        helpFrame.setLocationRelativeTo(null);

        JPanel helpPanel = new JPanel(new BorderLayout());
        helpPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setText(
                "帮助文档:\n\n" +
                "矿工大冒险是一款基于Java的矿工冒险游戏，玩家需要在一个地图上收集矿石，通过楼梯到达下一层。\n\n" +
                "场地中有着各种结构\n\n" +
                "- 铁矿石：消耗耐久度5\n" +
                "- 铜矿石：消耗耐久度10\n" +
                "- 银矿石：消耗耐久度15\n" +
                "- 金矿石：消耗耐久度20\n" +
                "- 钻石矿石：消耗耐久度25\n" +
                "墙：不可破坏结构。\n" +
                "得分规则：分数 = 矿石消耗耐久度 * 层数 * 10\n" +
                "初始稿子耐久度为100，可拾取场地中的维修工具恢复稿子40点耐久\n" +
                        "可通过“下”来进入下一层，总共只有十层矿洞。"

        );

        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(400, 300));
        helpPanel.add(scrollPane, BorderLayout.CENTER);

        helpFrame.add(helpPanel);
        helpFrame.setVisible(true);
    }

}
